import 'package:flutter/material.dart';

class StringTool {
  StringTool._();

  ///数字转万
  static String countFormat(int count) {
    String views = "";
    if (count > 9999) {
      views = "${(count / 10000).toStringAsFixed(1)}万";
    } else {
      views = count.toString();
    }
    return views;
  }

  ///时间转换将秒转换为分钟:秒
  static String durationTransform(int seconds) {
    int m = (seconds / 60).truncate();
    int s = seconds - m * 60;
    if (s < 10) {
      return '$m:0$s';
    }
    return '$m:$s';
  }

  /// 处理手机号码
  static String handlePhoneNo(String phoneNo) {
    if (phoneNo.isEmpty || phoneNo.length != 11) return "";

    return "${phoneNo.substring(0, 3)}****${phoneNo.substring(phoneNo.length - 4)}";
  }

  static String toStringOrEmpty(dynamic content) {
    if (content == null) return '';
    if (content is num) return '$content';
    return content?.toString() ?? '';
  }

  static bool isEmpty(String? content) {
    return content == null || content.isEmpty;
  }

  static bool isNotEmpty(String? content) {
    return !isEmpty(content);
  }

  static bool isBlank(String? content) {
    return content == null || content.trim().isEmpty || content == "";
  }

  /// 计算文字的宽高
  static Size boundingTextSize(String text, TextStyle style,
      {int maxLines = 2 ^ 31, double maxWidth = double.infinity}) {
    if (text.isEmpty) {
      return Size.zero;
    }

    final TextPainter textPainter = TextPainter(
        textDirection: TextDirection.ltr,
        text: TextSpan(text: text, style: style),
        maxLines: maxLines)
      ..layout(maxWidth: maxWidth);
    return textPainter.size;
  }

  static bool isJson(dynamic content) {
    if (content == null) return false;
    if (content is! String) return false;
    if (content.startsWith('{') && content.endsWith('}')) return true;
    if (content.startsWith('[') && content.endsWith(']')) return true;
    return false;
  }

  static bool equals(String? a, String? b) {
    if (a == null && b == null) {
      return true;
    } else if (a == null || b == null) {
      return false;
    } else if (a.compareTo(b) == 0) {
      return true;
    }
    return false;
  }

  static String formatJson(dynamic object, int deep, {bool isObject = false}) {
    var buffer = StringBuffer();
    var nextDeep = deep + 1;
    if (object is Map) {
      var list = object.keys.toList();
      if (!isObject) {
        //如果map来自某个字段，则不需要显示缩进
        buffer.write("  " * deep);
      }
      buffer.write("{");
      if (list.isEmpty) {
        //当map为空，直接返回‘}’
        buffer.write("}");
      } else {
        buffer.write("\n");
        for (int i = 0; i < list.length; i++) {
          buffer.write("  " * nextDeep);
          buffer.write("\"${list[i]}\":");
          buffer.write(formatJson(object[list[i]], nextDeep, isObject: true));
          if (i < list.length - 1) {
            buffer.write(",");
            buffer.write("\n");
          }
        }
        buffer.write("\n");
        buffer.write("  " * deep);
        buffer.write("}");
      }
    } else if (object is List) {
      if (!isObject) {
        //如果list来自某个字段，则不需要显示缩进
        buffer.write("  " * deep);
      }
      buffer.write("[");
      if (object.isEmpty) {
        //当list为空，直接返回‘]’
        buffer.write("]");
      } else {
        buffer.write("\n");
        for (int i = 0; i < object.length; i++) {
          buffer.write(formatJson(object[i], nextDeep));
          if (i < object.length - 1) {
            buffer.write(",");
            buffer.write("\n");
          }
        }
        buffer.write("\n");
        buffer.write("  " * deep);
        buffer.write("]");
      }
    } else if (object is String) {
      //为字符串时，需要添加双引号并返回当前内容
      buffer.write("\"$object\"");
    } else if (object is num || object is bool) {
      //为数字或者布尔值时，返回当前内容
      buffer.write(object);
    } else {
      //如果对象为空，则返回null字符串
      buffer.write("null");
    }
    return buffer.toString();
  }

  static String formatCountdownTimer(int seconds) {
    final day = (seconds ~/ 86400);
    final hour = (seconds % 86400 ~/ 3600);
    final minute = (seconds % 3600 ~/ 60);
    final second = (seconds % 60);
    if (day > 0) {
      return '$day天${hour.toString().padLeft(2, '0')}小时${minute.toString().padLeft(2, '0')}分${second.toString().padLeft(2, '0')}秒';
    } else if (hour > 0) {
      return '${hour.toString().padLeft(2, '0')}小时${minute.toString().padLeft(2, '0')}分${second.toString().padLeft(2, '0')}秒';
    } else {
      return '${minute.toString().padLeft(2, '0')}分${second.toString().padLeft(2, '0')}秒';
    }
  }
}
